var request = require('request'),
	cheerio = require('cheerio'),
	baseUrl = 'https://au.finance.yahoo.com/q/ks?s={0}',
	ticker = process.argv[2],
	url = baseUrl.replace('{0}', ticker),
	baseSpanId = '#yfs_j10_{0}';
	spanId = baseSpanId.replace('{0}', ticker);

req = request(url, function(err, resp, body) {
	var $ = cheerio.load(body);
	var $capSpan = $($(spanId).get(0));
	
	var cap = $capSpan.text();
	
	if (cap.length == 0) {
		return;
	}
	
	if (cap.indexOf('B') > 0) {
		cap = cap.replace('B', '');
		cap = parseFloat(cap) * 1000000000;
	} else if (cap.indexOf('M') > 0) {
		cap = cap.replace('M', '');
		cap = parseFloat(cap) * 1000000;
	}
	
	console.log(cap);
});

setTimeout(function(){
	req.abort();
}, 2000);